package cn.edu.sgu.www.mhxysy.controller.achievement;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.achievement.ChengweiDTO;
import cn.edu.sgu.www.mhxysy.entity.achievement.Chengwei;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.achievement.ChengweiService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "称谓管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/chengwei", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ChengweiController {

	private final ChengweiService chengweiService;

	@Autowired
	public ChengweiController(ChengweiService chengweiService) {
		this.chengweiService = chengweiService;
	}

	@ApiOperation("添加称谓")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated ChengweiDTO chengweiDTO) {
		chengweiService.insert(chengweiDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID批量删除称谓")
	@RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
	public JsonResult<Void> deleteByIds(
			@ApiParam(name = "ids", value = "称谓ID列表", required = true)
			@RequestParam("ids") List<String> ids) {
		chengweiService.deleteByIds(ids);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改称谓信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated ChengweiDTO chengweiDTO) {
		chengweiService.updateById(chengweiDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询全部称谓")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<Chengwei>> selectAll() {
		List<Chengwei> list = chengweiService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过ID查询称谓信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Chengwei> selectById(
			@ApiParam(name = "id", value = "称谓ID", required = true)
			@NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
		Chengwei chengwei = chengweiService.selectById(id);

		return JsonResult.success(null, chengwei);
	}

	@ApiOperation("查询角色的称谓列表")
	@RequestMapping(value = "/selectByRoleId", method = RequestMethod.GET)
	public JsonResult<List<Chengwei>> selectByRoleId(
			@ApiParam(name = "roleId", value = "角色ID", required = false)
			@RequestParam(required = false) String roleId) {
		List<Chengwei> list = chengweiService.selectByRoleId(roleId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("分页查询称谓列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<Chengwei>> selectByPage(Pager<Chengwei> pager) {
		Page<Chengwei> result = chengweiService.selectByPage(pager);

		return JsonResult.restPage(result);
	}

}